package com.ioffice.export.excel.engine.data.excel.impl.tree;

import java.util.List;

import org.apache.log4j.Logger;

import com.ioffice.exception.IOfficeException;
import com.ioffice.export.excel.data.ExcelSheetDataInfo;
import com.ioffice.export.excel.engine.data.excel.impl.AbstractEngineDataExcelSheet;
import com.ioffice.export.excel.engine.data.excel.impl.AbstractEngineDataSheetCompent;
import com.ioffice.export.excel.engine.parse.ParseExcelSheetTree;
import com.ioffice.export.excel.sheet.ExcelSheet;
import com.ioffice.export.excel.sheet.tree.ExcelSheetTree;
import com.ioffice.export.excel.sheet.tree.SheetCrossHeader;
import com.ioffice.export.excel.sheet.tree.SheetDataBody;
import com.ioffice.export.excel.sheet.tree.SheetLeftHeader;
import com.ioffice.export.excel.sheet.tree.SheetTitle;
import com.ioffice.export.excel.sheet.tree.SheetTopHeader;
import com.ioffice.export.excel.type.ExcelSheetTreeType;
import com.ioffice.export.excel.type.ExcelSheetType;

/**
 * 
 * @author <a href="mailto:goodluck.sunlight@gmail.com">chenboning</a>
 *
 * 
 * @descption 将ExcelSheetDataInfo转换为ExcelSheet对象
 *  
 *  <li>EngineDataExcelSheetTree充当数据与POI之间的转换器</li>
 *  <li>1</li>.将ExcelSheetDataInfo的数据经过计算和合并的相关计算生成ExcelSheet
 *  <li>2</li>.该模板将POi能解析的对象进行返回
 *
 * @version 1.0
 *
 *
 * @time  2013-5-21  下午1:59:25
 */
public class EngineDataExcelSheetTree extends AbstractEngineDataExcelSheet{

	private Logger log=Logger.getLogger(getClass());
	private String errorMsg="";
	
	private ParseExcelSheetTree parseExcelSheetTree=null;
	private boolean setDataBody=false;
	//SheetTitle
	private AbstractEngineDataSheetCompent dataSheetTitle=null;
	//sheetCrossHeader
	private AbstractEngineDataSheetCompent dataSheetCrossHeader=null;
	//sheetTopHeader
	private AbstractEngineDataSheetCompent dataSheetTopHeader=null;
	//sheetLeftHeader
	private AbstractEngineDataSheetCompent dataSheetLeftHeader=null;
	//sheetDataBoday
	private AbstractEngineDataSheetCompent dataSheetDataBoday=null;
	
	public EngineDataExcelSheetTree() {
		if(parseExcelSheetTree==null)
			parseExcelSheetTree=new ParseExcelSheetTree();
		if(dataSheetTitle==null)
			dataSheetTitle=new EngineDataSheetTreeTitle();
		if(dataSheetCrossHeader==null)
			dataSheetCrossHeader=new EngineDataSheetTreeCrossHeader();
		if(dataSheetTopHeader==null)
			dataSheetTopHeader=new EngineDataSheetTreeTopHeader();
		if(dataSheetLeftHeader==null)
			dataSheetLeftHeader=new EngineDataSheetTreeLeftHeader();
		if(dataSheetDataBoday==null)
			dataSheetDataBoday=new EngineDataSheetTreeDataBody();
			
	}
	/**
	 * 尽量使用该构造函数
	 * @param isSetData
	 */
	public EngineDataExcelSheetTree(boolean isSetData){
		this();
		this.setDataBody=isSetData;
	}
	
	
	
	@Override
	public void createExcelSheet(List<ExcelSheetDataInfo> datas,ExcelSheetType sheetType) throws IOfficeException {
		sheet=new ExcelSheetTree();
		
		//构建Sheet的基本结构
		createSheetBasicStructure((ExcelSheetTree)sheet, datas, sheetType);
		//计算sheet各个结构的span与坐标等信息
		parseExcelSheetTreeProperty((ExcelSheetTree)sheet,sheetType);
		//构建数据体育计算数据的span与坐标
		createSheetBasicStructureSheetDataBody((ExcelSheetTree)sheet, datas, sheetType);
		//计算整个sheet的span与坐标等信息
		parseExcelSheetTree.parseExcelSheetProperty((ExcelSheetTree)sheet, (ExcelSheetTreeType)sheetType);
	}
	
	/**
	 * 计算Sheet中的title、crossheader、topheader、leftheader的span与坐标等信息
	 * @param sheetTree
	 */
	private void parseExcelSheetTreeProperty(ExcelSheetTree sheetTree,ExcelSheetType sheetType){
		parseExcelSheetTree.parseSheetTitleProperty(sheetTree,sheetType.getTitleSpan());
		parseExcelSheetTree.parseSheetCrossHeaderProperty(sheetTree);
		parseExcelSheetTree.parseSheetTopHeaderProperty(sheetTree);
		parseExcelSheetTree.parseSheetTitleProperty(sheetTree);
		parseExcelSheetTree.parseSheetLeftHeaderProperty(sheetTree);
		parseExcelSheetTree.parseSheetLeftHeaderChildCellSpan((SheetLeftHeader)sheetTree.getSheetLeftHeader());
	}
	
	/**
	 * 构建Sheet的基本结构
	 * @param sheetTree
	 * @param datas
	 * @param sheetType
	 * @throws IOfficeException
	 */
	private void createSheetBasicStructure(ExcelSheetTree sheetTree,List<ExcelSheetDataInfo> datas,ExcelSheetType sheetType)throws IOfficeException{
		dataSheetTitle.data2SheetCompent(datas, sheetType);//
		dataSheetCrossHeader.data2SheetCompent(datas, sheetType);
		dataSheetTopHeader.data2SheetCompent(datas, sheetType);
		dataSheetLeftHeader.data2SheetCompent(datas, sheetType);
		
		//SheetTitle
		SheetTitle title=(SheetTitle)dataSheetTitle.getSheetGenericCompent();
		
		SheetTopHeader top=createSheetTopHeader();
		SheetCrossHeader cross=createSheetCrossHeader();
		//sheetLeftHeader
		SheetLeftHeader left=createSheetLeftHeader();
		
		sheetTree.setSheetTitle(title);
		sheetTree.setSheetCrossHeader(cross);
		sheetTree.setSheetTopHeader(top);
		sheetTree.setSheetLeftHeader(left);
		
	}
	
	/**
	 * 构建sheet的dataBody数据结构
	 * @param sheetTree
	 * @param datas
	 * @param sheetType
	 * @throws IOfficeException
	 */
	private void createSheetBasicStructureSheetDataBody(ExcelSheetTree sheetTree,List<ExcelSheetDataInfo> datas,ExcelSheetType sheetType)throws IOfficeException{
		SheetDataBody dataBody=createSheetDataBody(sheetTree, datas, sheetType);
		sheetTree.setSheetDataBody(dataBody);
		int rowCount=dataBody.getRowCount();
		if(!setDataBody&&rowCount>0)
			parseExcelSheetTree.parseSheetDataBodyProperty(sheetTree);
	}
	/**
	 * 构建Sheet的topHeader数据结构
	 * @return
	 * @throws IOfficeException
	 */
	private SheetTopHeader createSheetTopHeader()throws IOfficeException{
		//sheetTopHeader
		SheetTopHeader top=(SheetTopHeader)dataSheetTopHeader.getSheetGenericCompent();
		int topLeafCount=((EngineDataSheetTreeTopHeader)dataSheetTopHeader).getSheetTopHeaderNodesLeaf(top);
		top.setLeafCount(topLeafCount);
		
		return top;
	}
	/**
	 * 构建Sheet的crossHeader数据结构
	 * @return
	 * @throws IOfficeException
	 */
	private SheetCrossHeader createSheetCrossHeader()throws IOfficeException{
		//sheetCrossHeader 
		SheetCrossHeader cross=(SheetCrossHeader)dataSheetCrossHeader.getSheetGenericCompent();
		int crossLeafCount=((EngineDataSheetTreeCrossHeader)dataSheetCrossHeader).getSheetCrossHeaderNodesLeaf(cross);
		cross.setLeafCount(crossLeafCount);
		
		return cross;
	}
	
	/**
	 * 构建sheet的LeftHeader的数据结构
	 * @return
	 * @throws IOfficeException
	 */
	private SheetLeftHeader createSheetLeftHeader()throws IOfficeException{
		SheetLeftHeader left=(SheetLeftHeader)dataSheetLeftHeader.getSheetGenericCompent();
		left.setLeafCount(((EngineDataSheetTreeLeftHeader)dataSheetLeftHeader).getSheetLeftHeaderNodesLeaf(left));
		return left;
	}
	
	/**
	 * 构建Sheet的dataBody数据结构
	 * @param sheet
	 * @param datas
	 * @param sheetType
	 * @return
	 * @throws IOfficeException
	 */
	private SheetDataBody createSheetDataBody(ExcelSheetTree sheet,List<ExcelSheetDataInfo> datas,ExcelSheetType sheetType)throws IOfficeException{
		int colCount=((ExcelSheetTree)sheet).getSheetTopHeader().getXspan();//列数
		int rowCount=((ExcelSheetTree)sheet).getSheetLeftHeader().getYspan();//行数
		((EngineDataSheetTreeDataBody)dataSheetDataBoday).setColCount(colCount);
		((EngineDataSheetTreeDataBody)dataSheetDataBoday).setRowCount(rowCount);
		dataSheetDataBoday.data2SheetCompent(datas, sheetType);
		return (SheetDataBody)dataSheetDataBoday.getSheetGenericCompent();
	}
}
